<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="js/react.development.js"></script>
    <script type="text/javascript" src="js/react-dom.development.js"></script>
    <script type="text/javascript" src="js/babel.min.js"></script>
    <style>
        div{
            width: 600px;
            margin: 50px auto;
        }
        p,ul{
            width: 600px;
            padding: 0;
        }
        ul>li{
            list-style: none;
            width: 600px;
            overflow: hidden;
            border-bottom: 1px solid #ccc;
        }
        ul>li span:nth-child(1){
            float: left;
        }
        ul>li span:nth-child(2){
            float: right;
            color: royalblue;
        }
        p{
            display: flex;
        }
        p>input{
            flex: 1;
        }
    </style>
</head>
<body>
    <div id="root">
    
    </div>
    <script type="text/babel">
        class Mydemo extends React.Component{
            constructor(props) {
                super(props);
                this.state = {
                    arrList:['1','2','3'],
                    str:'',
                }
            }

            inputFn=(e)=>{
                this.setState({
                    str: e.target.value
                })
            }

            enterFn=(e)=>{
                e.persist()
                if(e.keyCode === 13) {
                    this.add();
                }
                // console.log(e);
            }

            // 箭头函数不会改变原来的this指向
            add=()=>{
                // console.log(this);
                if(this.state.str.trim().length<=0) {
                    return;
                }
                // let newArr = this.state.arrList.slice();
                // newArr.push(this.state.str);
                // this.setState({
                //     arrList: newArr,
                //     str:''
                // })

                // setState方法可以传参数state
                this.setState(state=>{
                    return {
                        arrList: [...state.arrList, state.str],
                        str:''
                    }
                })
            }

            delete(i) {
                // console.log(this,i);
                let newArr = this.state.arrList.slice();
                newArr.splice(i,1)
                this.setState({
                    arrList: newArr
                })
            }

            render() {
                return (
                    <div>
                        <h2>To do list</h2>
                        <p>
                            <input onKeyDown={this.enterFn} onChange={this.inputFn} type="text" value={this.state.str} name="str" id=""/><button onClick={this.add}>增加</button>
                        </p>
                        <ul>
                            {
                                this.state.arrList.map((item,index)=><li key={index}><span>{item}</span><span onClick={()=>this.delete(index)}>删除</span></li>)
                            }
                        </ul>
                    </div> 
                )
            }
        }

        
        ReactDOM.render(<Mydemo/>, document.getElementById('root'));
    </script>
</body>
</html>